
package com.huaqin.excel.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

import com.huaqin.excel.beans.ContentBean;
import com.huaqin.excel.handler.CommonFileHandler;
import com.huaqin.excel.handler.ExcelDataHandler;
import com.huaqin.excel.handler.ExcelMainHandler;
import com.huaqin.excel.util.InternationalUtils;

public class MainFrame extends JFrame
{
	private JPanel mainPane = new JPanel(new BorderLayout());
	

	private static final String welcome = InternationalUtils
			.getStringFromProperties("welcome");

	public MainFrame(String path) throws HeadlessException {
		super(welcome);

		initGUI();

		setSize(600, 400);
	}
	
	private void initGUI()
	{
		
		this.setJMenuBar(createMenuBar());
		createMainLayout();
		
	}
	
	
	
	
//	private JComponent createMainLayout() {
//		
//		JPanel productPanel = new JPanel();
//
//		GridBagLayout layout = new GridBagLayout();
//		productPanel.setLayout(layout);
//		
//		JLabel productLabel = new JLabel("product:");
//		JTextField productTxt = new  JTextField(20);
//		
//		
//		JLabel languageLabel = new JLabel("language:");
//		JComboBox languageBox = new JComboBox();
//		for(int i = 0; i < operators.length; i++) {
//			languageBox.addItem(operators[i]);
//		}
//		
//		
//		
//		JLabel operatorLabel=new JLabel("operators:");
//		
//		
//		JComboBox comboBox = new JComboBox();
//		for(int i = 0; i < operators.length; i++) {
//			comboBox.addItem(operators[i]);
//		}
//		
//		
//		
//		JLabel countryLabel=new JLabel("country:");
//		
//		
//		JComboBox comboBox1 = new JComboBox();
//
//		for(int i = 0; i < countrys.length; i++) {
//			comboBox1.addItem(countrys[i]);
//		}
//		
//		
//		productPanel.add(productLabel, new GBC(0, 0).setAnchor(GBC.WEST));
//		productPanel.add(productTxt, new GBC(1, 0).setFill(GBC.WEST).setWeight(100, 0)
//				.setInsets(100));
//
//		productPanel.add(operatorLabel, new GBC(0, 1).setAnchor(GBC.WEST).setWeight(100, 0));
//		productPanel.add(comboBox, new GBC(1, 1).setFill(GBC.WEST).setWeight(100, 0)
//				.setInsets(10));
//		productPanel.add(countryLabel, new GBC(2, 1).setAnchor(GBC.WEST).setWeight(100, 0));
//		productPanel.add(comboBox1, new GBC(3, 1).setFill(GBC.WEST).setWeight(100, 0)
//				.setInsets(10));
//
//		productPanel.add(languageLabel, new GBC(0, 2).setAnchor(GBC.WEST).setWeight(100, 0).setInsets(2));
//		productPanel.add(languageBox,
//				new GBC(1, 2).setAnchor(GBC.CENTER).setWeight(100, 0).setInsets(2));
//		
//		
//		return productPanel;
//	}
	
	public JComboBox languageBox = null;
	public JTextField productTxt = null;
	public JComboBox  operatorBox = null;
	public JComboBox countryBox = null;
	public JTextField updateFilePathTxt = null;
	public JTextField  authorTxt = null;
	
	public JTextField videoPathTxt = null;
	
	//selectFiles save the files that we click the select button
	//we can select a file or a dictionary or both
	public File[] selectFiles = null;
	//save the path of selectFiles
	public ArrayList<String> selectFilesPath = null;
	//save the name of selectFiles
	public ArrayList<String> selectFilesName = null;
	
	//save the boot video
	public File[] selectVideo = null;
	public ArrayList<String> selectVideoPath = null;
	public ArrayList<String> selectVideoName = null;
	
private void createMainLayout() {
		
//		JPanel productPanel = new JPanel();
		ActionListener listener = new GeneralAction();
		GridBagLayout layout = new GridBagLayout();
		this.setLayout(layout);

		String product_ = InternationalUtils.getStringFromProperties("product");

		JLabel productLabel = new JLabel(product_ + ":");
		productTxt = new JTextField(20);

		String language_ = InternationalUtils
				.getStringFromProperties("language");
		JLabel languageLabel = new JLabel(language_ + ":");

		languageBox = new JComboBox();
		for(int i = 0; i < languages.length; i++) {
			languageBox.addItem(languages[i]);
		}
		languageBox.setEditable(true);

		String operators_ = InternationalUtils
				.getStringFromProperties("operators");
		JLabel operatorLabel = new JLabel(operators_ + ":");

		operatorBox = new JComboBox();
		for(int i = 0; i < operators.length; i++) {
			operatorBox.addItem(operators[i]);
		}
		operatorBox.setEditable(true);

		String country_ = InternationalUtils.getStringFromProperties("country");
		JLabel countryLabel = new JLabel(country_ + ":");

		countryBox = new JComboBox();

		for(int i = 0; i < countrys.length; i++) {
			countryBox.addItem(countrys[i]);
		}
		countryBox.setEditable(true);

		updateFilePathTxt = new JTextField(20);
		String Select_ = InternationalUtils.getStringFromProperties("Select");
	    JButton pathButton = new JButton(Select_);
		
		pathButton.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				//System.out.println("pathButton");
				JFileChooser fileChooser = new JFileChooser(".");
				fileChooser.setMultiSelectionEnabled(true);
				fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
				fileChooser.showOpenDialog(null);
				selectFiles = fileChooser.getSelectedFiles();
				if (selectFiles==null||selectFiles.length==0) {
					Object[] options = { "OK", "CANCEL" };
					JOptionPane.showOptionDialog(null, "Nothing has selected", "Warning",
					JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
					null, options, options[0]);
					return;
				}
				
				selectFilesPath = new ArrayList<String>();
				selectFilesName = new ArrayList<String>();
				StringBuffer sBuffer = new StringBuffer();
				for (int i = 0; i < selectFiles.length; i++) {
					
					if (selectFiles[i].isFile()) {
						selectFilesPath.add(selectFiles[i].getAbsolutePath());
						selectFilesName.add(selectFiles[i].getName());
					}
					sBuffer.append(selectFiles[i].getAbsolutePath()+",");
					
				}
				updateFilePathTxt.setText(sBuffer.toString());
				ContentBean.getInstance().setSelectFilesPath(selectFilesPath);
				ContentBean.getInstance().setSelectFilesName(selectFilesName);
//				for (int j = 0; j < selectFilesPath.size(); j++) {
//					System.out.println(selectFilesPath.get(j));
//				}
			}
		});
		

		videoPathTxt = new JTextField(20);
		String Select_Video = InternationalUtils.getStringFromProperties("Select_video");
	    JButton video_Button = new JButton(Select_Video);
	    
	    video_Button.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
//				System.out.println("video_Button");
				JFileChooser fileChooser = new JFileChooser(".");
				fileChooser.setMultiSelectionEnabled(true);
				fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
				fileChooser.showOpenDialog(null);
				selectVideo = fileChooser.getSelectedFiles();
				if (selectVideo==null||selectVideo.length==0) {
					Object[] options = { "OK", "CANCEL" };
					JOptionPane.showOptionDialog(null, "Nothing has selected", "Warning",
					JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
					null, options, options[0]);
					return;
				}
				
				selectVideoPath = new ArrayList<String>();
				selectVideoName = new ArrayList<String>();
				StringBuffer sBuffer = new StringBuffer();
				for (int i = 0; i < selectVideo.length; i++) {
					
					if (selectVideo[i].isFile()) {
						selectVideoPath.add(selectVideo[i].getAbsolutePath());
						selectVideoName.add(selectVideo[i].getName());
					}
					sBuffer.append(selectVideo[i].getAbsolutePath()+",");
					
				}
				videoPathTxt.setText(sBuffer.toString());
				ContentBean.getInstance().setSelectVideoPath(selectVideoPath);
				ContentBean.getInstance().setSelectVideoName(selectVideoName);
//				for (int j = 0; j < selectVideoPath.size(); j++) {
//					System.out.println(selectVideoPath.get(j));
//				}
//				for (int j = 0; j < selectVideoName.size(); j++) {
//					System.out.println(selectVideoName.get(j));
//				}
			}
		});

		String Auther_ = InternationalUtils.getStringFromProperties("Auther");
		JLabel authorLabel = new JLabel(Auther_ + ":");
		authorTxt = new JTextField(20);

		// General zip
		String General_zip_ = InternationalUtils
				.getStringFromProperties("General_zip");
		JButton generalButton = new JButton(General_zip_);
		generalButton.addActionListener(listener);

		this.setResizable(false);

		this.add(productLabel, new GBC(0, 0, 1, 1).setAnchor(GBC.EAST)
				.setInsets(10));
		this.add(productTxt, new GBC(1, 0, 2, 1).setFill(GBC.NONE)
				.setInsets(10));

		this.add(operatorLabel, new GBC(0, 1, 1, 1).setAnchor(GBC.EAST)
				.setInsets(10));
		this.add(operatorBox, new GBC(1, 1, 1, 1).setAnchor(GBC.WEST)
				.setInsets(10));

		this.add(countryLabel, new GBC(0, 2, 1, 1).setAnchor(GBC.EAST)
				.setInsets(10));
		this.add(countryBox,
				new GBC(1, 2, 1, 1).setAnchor(GBC.WEST).setInsets(10));

		this.add(languageLabel, new GBC(0, 3, 1, 1).setAnchor(GBC.EAST)
				.setInsets(10));
		this.add(languageBox, new GBC(1, 3, 1, 1).setAnchor(GBC.WEST)
				.setInsets(10));

		this.add(updateFilePathTxt, new GBC(0, 4, 1, 1).setAnchor(GBC.EAST)
				.setInsets(10));
		this.add(pathButton,
				new GBC(1, 4, 1, 1).setAnchor(GBC.WEST).setInsets(10));

		this.add(videoPathTxt, new GBC(0, 5, 1, 1).setAnchor(GBC.EAST)
				.setInsets(10));
		this.add(video_Button, new GBC(1, 5, 1, 1).setAnchor(GBC.WEST)
				.setInsets(10));

		this.add(authorLabel, new GBC(0, 6, 1, 1).setAnchor(GBC.EAST)
				.setInsets(10));
		this.add(authorTxt,
				new GBC(1, 6, 1, 1).setAnchor(GBC.WEST).setInsets(10));

		this.add(generalButton, new GBC(1, 7, 1, 1).setAnchor(GBC.WEST)
				.setInsets(10));
	}

	public String[] operators = { "OpenMarket", "ChinaMobile", "ChinaTelecom",
			"Claro", "Tigo", "Movistar", "Telcel", "ATT", "TIM", "MTN",
			"Turkcell", "CNT", "MTS", "Telkom", "Entel" };
	public String[] languages = { "Chinese", "English(United States)",
			"English", "Latin American Spainish", "Russian" };
	public String[] countrys = { "China", "Italy", "Porland", "Germany",
			"Myanmar", "SouthAfrica", "Mexico", "ElSalvador", "America",
			"Russia", "Turkey", "Columbia", "Kenya", "Ecuador", "Belarus",
			"Indonesia" };
	
	private JMenuBar createMenuBar() 
	{
		
		//------------------------------------ About
		JMenu aboutMenu = new JMenu("About");
		JMenuItem aboutMenuItem = new JMenuItem("About project");
		aboutMenuItem.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				JOptionPane.showMessageDialog(rootPane, "checklist" +
						"\n - is a auto runner validate checklist tools" +
						"\n - this project develop at 20160612" +
						"\n - author is guojianhui&taojin&zhongshengbin." +
						"\n");
			}
		});
		aboutMenu.add(aboutMenuItem);
		
		JMenuBar menuBar = new JMenuBar();
//		menuBar.add(fileMenu);
//		menuBar.add(fileMenu2);
		menuBar.add(aboutMenu);
		
		return menuBar;
	}
	
	/**
	 * @author guojianhui
	 *
	 */
	private class GeneralAction implements ActionListener {
	    public void actionPerformed(ActionEvent event) {
	        
	    	//get string
	    	
	    	setInputProductName();
	    	setInputOperatorName();
	    	setInputLanguageName();
	    	setInputCountryName();
	    	setUpdateFilePath();
	    	setVideoInputPath();
	    	setInputAuthor();
	    	
			// valide chip
			if (InternationalUtils.isEmpty(getInputProductName())) {
				showMessageDialog(getErrorMessage(ERROR_CODE.MESSAGE_PRODUCTNAME_NULL));
				return;
			}

			if (InternationalUtils.isEmpty(getupdateFilePath())) {
				showMessageDialog(getErrorMessage(ERROR_CODE.MESSAGE_UPDATEFILE_NULL));
				return;
			}

			if (InternationalUtils.isEmpty(getVideoPath())) {
				showMessageDialog(getErrorMessage(ERROR_CODE.MESSAGE_VIDEOPATH_NULL));
				return;
			}

			if (InternationalUtils.isEmpty(getInputAuthor())) {
				showMessageDialog(getErrorMessage(ERROR_CODE.MESSAGE_AUTHOR_NULL));
				return;
			}

	    	ExcelDataHandler.getInstance().exePropValue("adb shell getprop ro.huaqin.internal.version");
	    	String hq_version = ExcelDataHandler.getInstance().getPropValue();
//	    	System.out.println("guo content interal version: "+getInputProductName());
//	    	System.out.println("guo prop interal version: "+hq_version);
	    	if(InternationalUtils.isEmpty(hq_version)) {
				showMessageDialog(getErrorMessage(ERROR_CODE.MESSAGE_CONN));
				return ;	
			}
			
			if (!InternationalUtils.equals(
					getInputProductName(),
					hq_version)) {
				showMessageDialog(getErrorMessage(ERROR_CODE.MESSAGE_EQUALS));
	    		return ;	
			}
	    	
			//clear global data 
	    	FinalizeDataObject();
	    	//make global data
			ExcelMainHandler.getInstance().makeGlobalData();
	    	//main run chip
	    	ExcelMainHandler.getInstance().mainRuning();
	    	
	    	showMessageDialog(getErrorMessage(ERROR_CODE.MESSAGE_OK));
	    	
	    }
	}
	
	//free sington object ,obtain data sync
	public void FinalizeDataObject() {
		ExcelMainHandler.getInstance().free();
		ExcelDataHandler.getInstance().free();
		CommonFileHandler.getInstance().free();
	}
	
	
	public void CheckInfo() {
		//TAG-L22C567B131_D
    	ExcelDataHandler handler = ExcelDataHandler.getInstance();
//    	System.out.println("taojin" + ContentBean.getInstance().getProduct().toString() + "////" + handler.getPropHashMap("ro.huaqin.internal.version"));
    	
//    	JLabel displayCheckInfoLabel=new JLabel("checkinfo:");
//    	this.add(displayCheckInfoLabel, new GBC(1, 5).setAnchor(GBC.WEST).setWeight(100, 0).setInsets(2));
    	
    	//judge fingerprint from getprop is correct or not
//    	if(handler.getPropHashMap("ro.build.version.incremental").equals(handler.getPropHashMap("ro.huaqin.internal.version"))){
//    		
//    	}else{
//    		
//    	}
    	
    	    	
    	if(ContentBean.getInstance().getProduct().toString().equals(handler.getPropHashMap("ro.huaqin.internal.version"))){	    		
//	    	System.out.println("taojin2" + ContentBean.getInstance().getProduct().toString() + "////" + handler.getPropHashMap("ro.huaqin.internal.version"));
	    	//displayCheckInfoLabel.setText("successful!");
	    	Object[] options = { "OK", "CANCEL" }; 
	    	JOptionPane.showOptionDialog(null, "check info successful", "Warning", 
	    	JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, 
	    	null, options, options[0]); 
    	}else{
    		//displayCheckInfoLabel.setText("Version info is Error!");
    		Object[] options = { "OK", "CANCEL" }; 
	    	JOptionPane.showOptionDialog(null, "Version info is Error!", "Warning", 
	    	JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, 
	    	null, options, options[0]);
    	}
	}
	
	enum ERROR_CODE {
		MESSAGE_PRODUCTNAME_NULL, MESSAGE_EQUALS, MESSAGE_CONN, MESSAGE_OK, MESSAGE_AUTHOR_NULL, MESSAGE_UPDATEFILE_NULL, MESSAGE_VIDEOPATH_NULL
	}
	
	/**valide ui input data is corret
	 * @return
	 */
	public String getErrorMessage (ERROR_CODE code) {
		
		String message = "";
		
		switch (code) {
			case MESSAGE_PRODUCTNAME_NULL:
				message = "Product name is null,please write!!";
				break;
			case MESSAGE_UPDATEFILE_NULL:
				message = "Update files is null,please load!!";
				break;
			case MESSAGE_VIDEOPATH_NULL:
				message = "Video file is null,please load!!";
				break;
			case MESSAGE_AUTHOR_NULL:
				message = "Author is null,please write!!";
				break;
			case MESSAGE_EQUALS:
				message = "product name is not correct,please rewrite!!";
				break;
			case MESSAGE_CONN:
				message = "please connect devices!!";
				break;
			case MESSAGE_OK:
				System.out.println("make complete successfully");
				message = "make ok!!!";
				break;
		}
		
		return message;
	}
	
	/**show message dialog
	 * @param message
	 */
	public void showMessageDialog(String message) {
		JOptionPane.showMessageDialog(rootPane, message);
	}
	
	
	public String getInputProductName() {
		
		
		return ContentBean.getInstance().getProduct();
	}
	
	public String getInputOperatorName() {
		
		
		return ContentBean.getInstance().getOperator();
	}
	
	public String getInputCountryName() {
		return ContentBean.getInstance().getCountry();
	}
	
	
	public String getInputLanguageName() {
		return ContentBean.getInstance().getLanguage();
	}
	
	public String getupdateFilePath() {
		return ContentBean.getInstance().getupdatefilePath();
	}
	
	public String getVideoPath() {
		return ContentBean.getInstance().getvideofilePath();
	}
	
	public String getInputAuthor() {
		return ContentBean.getInstance().getAuthor();
	}
	
	public void setInputProductName() {
		
		if(productTxt == null) {
			return;
		}
//		System.out.print("languageBox.getName():"+productTxt.getText());
		ContentBean.getInstance().setProduct(productTxt.getText());
	}
	
	public void setInputOperatorName() {
		
		if(operatorBox == null) {
			return ;
		}
//		System.out.print("operatorBox.getName():"+operatorBox.getSelectedItem().toString());
//		mBeans.operator =  operatorBox.getSelectedItem().toString();
		ContentBean.getInstance().setOperator(operatorBox.getSelectedItem().toString());
	}
	
	public void setInputCountryName() {
		
		if(countryBox == null) {
			return;
		}
//		System.out.print("languageBox.getName():"+countryBox.getSelectedItem().toString());
//		mBeans.country =  countryBox.getSelectedItem().toString();
		ContentBean.getInstance().setCountry(countryBox.getSelectedItem().toString());
	}
	
	public void setInputLanguageName() {
			
			if(languageBox == null) {
				return;
			}
//			System.out.print("languageBox.getName():"+languageBox.getSelectedItem().toString());
//			mBeans.language = languageBox.getSelectedItem().toString();
			ContentBean.getInstance().setLanguage(languageBox.getSelectedItem().toString());
		}
	
	public void setUpdateFilePath() {

		if (updateFilePathTxt == null) {
			return;
		}
//		System.out.print("updateFilePathTxt.getName():" + updateFilePathTxt.getText());
		// mBeans.path = updateFilePathTxt.getText();
		ContentBean.getInstance().setupdatefilePath(updateFilePathTxt.getText());
	}

	public void setVideoInputPath() {

		if (videoPathTxt == null) {
			return;
		}
//		System.out.print("videoPathTxt.getName():" + videoPathTxt.getText());
		// mBeans.path = updateFilePathTxt.getText();
		ContentBean.getInstance().setvideofilePath(videoPathTxt.getText());
	}

	public void setInputAuthor() {
		
		if(authorTxt == null) {
			return;
		}
//		System.out.print("authorTxt.getName():"+authorTxt.getText());
//		mBeans.author = authorTxt.getText();
		ContentBean.getInstance().setAuthor(authorTxt.getText());
	}
	

}
